home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5896 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  47 lines

  1. Newsgroups: comp.lang.c
  2. Path: news.sprintlink.net!news1!news
  3. From: rclark@iquest.net (Robert B. Clark)
  4. Subject: Re: C beginner needs your help ASAP
  5. X-Nntp-Posting-Host: ind-004-236-169.iquest.net
  6. Message-ID: <3129feba.6644222@news.iquest.net>
  7. Sender: news@iquest.net (News Admin)
  8. Organization: IQuest Internet, Inc.
  9. X-Newsreader: Forte Agent .99d/16.182
  10. References: <4g862f$p0b@risky.ecs.umass.edu> <4g8ahd$p8b@spectator.cris.com>
  11. Date: Tue, 20 Feb 1996 17:03:00 GMT
  12.  
  13. On 18 Feb 1996 22:51:25 GMT, aubrey@concentric.net (Aubrey Harrison)
  14. wrote:
  15.  
  16. >        char buf[3],filename[9];
  17. >        int  i;
  18. >
  19. >        for(i=0; i<10; i++)
  20. >        {
  21. >                sprintf(buf,"%d",i);
  22. >                strcpy( filename,"data");
  23. >                strcat( filename, buf );
  24. >                strcat( filename, ".ext");
  25. >                printf( "%s\n", filename);
  26. >        }
  27.  
  28. Just a quick word--are you aware that non-format type characters in the
  29. format pattern string for sprintf() are simply copied verbatim to the
  30. target string?  That is, for
  31.  
  32.     sprintf(buf,"This is numeral one (%d)",1)
  33.  
  34. 'buf' will contain
  35.  
  36.     This is numeral one (1)
  37.  
  38. So your code could have been simplified to
  39.  
  40.     sprintf(filename,"data%d.ext",i);    /* Returns e.g. "data0.ext"  */
  41.  
  42. if 'filename' was of sufficient length.
  43. --
  44. Robert B. Clark <rclark@iquest.net>
  45. "Be wary of strong spirits.  It can make you shoot at tax collectors...
  46. and miss." --RAH
  47.